/*
 * Copyright (c) 2006-2021, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-19     ZXJ        first version
 */
#include <rtthread.h>
#include "app_lcd_drv.h"

#include "app_lcd_drv.h"
#include "app_lcd_util.h"
#include "app_lcd_mp.h"
#include "app_lcd_sm.h"
#include "a5158_lcd_drv.h"
#include <drv_gpio.h>
#include <pin.h>

#ifndef LOG_TAG
#define LOG_TAG                    "app_lcd_task"
#endif
#undef LOG_LVL
#define LOG_LVL                    	LOG_LVL_INFO
#include "ulog.h"

#define LCD_POWER                    GET_PIN(C, 9)      

os_mq_t app_lcd_msg_handle;    //消息句柄 
static struct rt_timer client_timer; //线程定时器


/* 定时器  超时函数 */
static void lcd_client_timer_bck(void *parameter)
{
  	app_lcd_timer_msg_post();
	//log_i("lcd_client_timer()");
}

/*lcd线程*/
static void lcd_client_entry(void *param)
{
	//打开LCD电源
	rt_pin_mode(LCD_POWER , PIN_MODE_OUTPUT);
	rt_pin_write(LCD_POWER, TRUE);

    //初始化定时器
		rt_kprintf("func:%s, line:%d\r\n", __FUNCTION__, __LINE__);
    rt_timer_init(&client_timer, "lcd_client_timer",  
                    lcd_client_timer_bck, //超时时回调的处理函数
                    RT_NULL, // 超时函数的入口参数 
                    500, //定时长度，以 OS Tick 为单位，即 500 个 OS Tick 
                    RT_TIMER_FLAG_PERIODIC); //周期定时器

	if( RT_EOK!=rt_timer_start(&client_timer) )  //启动定时器
	{
		LOG_E("lcd_client_entry(err:timer start)");
	}

	//创建消息
	app_lcd_msg_handle = os_mq_create("app_lcd_msg_handle",256);//100
	if(app_lcd_msg_handle==RT_NULL){
		LOG_E("os_mq_create(err:app_lcd_msg_handle)");
		return;
	}

	if (!drv_lcd_init()){
		LOG_E("drv_lcd_init(err)");	
		return;
	}

	if (!app_lcd_msg_tab_init()){
		LOG_E("app_lcd_msg_tab_init(err)");	
		return;
	}

	app_lcd_sm_init();

	os_mq_data msg={0};
    while(1)
    {
		//rt_thread_mdelay(1000);
		app_lcd_rx_listen();

		memset(&msg, 0, sizeof(os_mq_data));
		if(os_mq_recv(app_lcd_msg_handle,&msg, 100))  //RT_WAITING_FOREVER
		{		
			app_lcd_msg_proc(&msg);   //消息处理
		} 
		os_mq_free_mem(&msg);  //释放消息空间
    }
}

static void lcd_uart_thread_entry(void *param)
{
	//打开LCD电源
	rt_pin_mode(LCD_POWER , PIN_MODE_OUTPUT);
	rt_pin_write(LCD_POWER, TRUE);

	if (!a5158_drv_lcd_init()){
		LOG_E("a5158_drv_lcd_init(err)");	
		return;
	}

    while(1)
    {
		a5158_lcd_rx_listen();
    }
}

static int32_t lcd_uart_task(void)
{
	rt_thread_t lcd_uart_thread;  //线程

	//创建并开启线程
	lcd_uart_thread = rt_thread_create("lcd", lcd_uart_thread_entry, RT_NULL, 2048, 18, 20);
	if(lcd_uart_thread){
		log_d("Start  lcd");
		rt_thread_startup(lcd_uart_thread);
	}else{
		LOG_E("rt_thread_create(err:lcd)");
		return -1;
	}
	return 0;
} 

#if defined(A5158_EDITION)
static int32_t app_lcd_task(void)
{
    if (lcd_uart_task() != RT_EOK){
        LOG_E("lcd uart task init fail.");
        return -RT_ERROR;
    }

	if (a5158_lcd_drive_task() != RT_EOK){
        LOG_E("lcd drive task init fail.");
        return -RT_ERROR;
    }
	return 0;
}
#else
static int32_t app_lcd_task(void)
{
	rt_thread_t lcd_client;  //线程


	//创建并开启线程
	lcd_client = rt_thread_create("lcd", lcd_client_entry, RT_NULL, 2048, 18, 20);
	if(lcd_client){
		log_d("Start  lcd");
		rt_thread_startup(lcd_client);
	}else{
		LOG_E("rt_thread_create(err:lcd)");
		return -1;
	}

	return 0;
}
#endif

INIT_ENV_EXPORT(app_lcd_task);